package cn.xej.health.archives.service.impl;

import cn.xej.health.archives.dao.ArchivesDao;
import cn.xej.health.archives.entity.Archives;
import cn.xej.health.archives.feign.TeamFeignService;
import cn.xej.health.archives.feign.UserFeignService;
import cn.xej.health.archives.service.ArchivesService;
import cn.xej.health.common.utils.CommonResult;
import cn.xej.health.common.utils.PageUtils;
import cn.xej.health.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xej
 */
@Service
public class ArchivesServiceImpl extends ServiceImpl<ArchivesDao, Archives> implements ArchivesService {

    @Autowired
    private ArchivesDao archivesDao;

    @Autowired
    private TeamFeignService teamFeignService;

    @Autowired
    private UserFeignService userFeignService;

    /**
     * 获取我的档案
     * @param uuid
     * @param pageIndex
     * @param pageSize
     * @param searchName
     * @return
     */
    @Override
    public CommonResult queryList(Integer uuid,Integer pageIndex, Integer pageSize, String searchName) {
        Map<String,Object> map = new HashMap<>();
        int startNum = (pageIndex-1)*pageSize+1;
        int endNum = pageIndex*pageSize;
        List<Archives> archivesList = archivesDao.queryArchivesList(uuid,startNum,endNum,searchName);
        QueryWrapper<Archives> queryWrapper = new QueryWrapper<Archives>().eq("uuid", uuid);
        queryWrapper.and(wrapper -> {
            wrapper.like("arch_name",searchName).or().like("resident",searchName);
        });
        Integer count = archivesDao.selectCount(queryWrapper);

        map.put("data",archivesList);
        map.put("total",count);
        return CommonResult.success(map);
    }

    /**
     * 获取档案根据档案编号
     * @param archid
     * @return
     */
    @Override
    public CommonResult info(Integer archid) {
        Map<String,Object> map = new HashMap<>();
        Archives archives = archivesDao.selectById(archid);
        CommonResult result = userFeignService.getUser(archives.getUuid());
        map.put("archives",archives);
        map.put("data",result.getData());
        return CommonResult.success(map);
    }

    /**
     * 获取区划
     * @return
     */
    @Override
    public CommonResult queryDivisionList() {
        return teamFeignService.divisionList();
    }

    /**
     * 获取团队树
     * @param did
     * @return
     */
    @Override
    public CommonResult queryTeamTree(Integer did) {
        return teamFeignService.teamTree(did);
    }

    /**
     * 获取该团队的成员
     * @param tid
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult initDockerList(Integer tid,Integer pageIndex,Integer pageSize) {
        CommonResult result = teamFeignService.getTeamDockerList(tid,pageIndex,pageSize);
        return CommonResult.success(result.getData());
    }

    /**
     * 检查居民的身份证
     * @param card
     * @return
     */
    @Override
    public CommonResult checkCard(String card) {
        Integer count = archivesDao.selectCount(new QueryWrapper<Archives>().eq("card", card));
        return count > 0 ? CommonResult.failed() : CommonResult.success(null);
    }
}
